/* =============================================================
 * h5_toast_ysy.js v1.1.0
 * =============================================================
 * Original written by Ysy
 * =============================================================
 * Date 23/02/2020
 * ============================================================ */


(function (factory) {
    "use strict";
    if (typeof define === 'function' && define.amd) {
        define(['jquery'], factory);
    } else {
        factory(jQuery);
    }
}(function ($) {

    let ToastTip = function (options) {
        this.options = $.extend({}, $.fn.toastTip.defaults, options);
        this.$main = $(this.options.main);
        this.render();
    };

    ToastTip.prototype = {

        constructor: ToastTip,

        render: function () {
            this.$main.html(this.options.message);
            this.$main.addClass("toastFadeIn");
            $("body").append(this.$main);
            setTimeout(() => {
                this.hide();
                this.listen();
            }, this.options.time);
        },

        hide: function () {
            this.$main.removeClass("toastFadeIn");
            this.$main.addClass("toastFadeOut");
        },

        listen: function () {
            this.$main.on("animationend", () => {
                this.$main.detach();
            }).on("webkitAnimationEnd", () => {
                this.$main.detach();
            });
        }
    };

    let old = $.fn.toastTip;

    $.fn.toastTip = function (options) {
        return new ToastTip(options);
    };

    $.fn.toastTip.defaults = {
        main: "<div class='web-toast'></div>",
        message: "",
        time: 2000,
    };

    $.fn.toastTip.Constructor = ToastTip;

    $.fn.toastTip.noConflict = function () {
        $.fn.toastTip = old;
        return this;
    };
}));
